package cn.springcloud.book.service;

import cn.springcloud.book.dao.DemoDao2;
import cn.springcloud.book.entity.DemoEntity;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.annotation.TxTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ServiceA {

    @Autowired
    private DemoDao2 demoDao; //本地db操作

//    @Autowired
//    private ServiceB serviceB;//远程B模块业务

    //https://www.txlcn.org

//    @LcnTransaction //分布式事务注解
    @Transactional //本地事务注解
//    @TxTransaction
    public String execute(String value) throws Exception {
        // step1. call remote service B
        //String result = serviceB.rpc(value);  // (1)
        // step2. local store operate. DTX commit if save success, rollback if not.
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setId(value);
        demoEntity.setName("lisi");
        demoDao.insert(demoEntity);  // (2)
        long a = System.currentTimeMillis();
        if (a % 2 == 1) {
            String b = null;

            log.info("模拟异常");
            log.info(b.toString());
        }
        return "ok";
    }
}
